package com.erp.order.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.order.service.IErpSellerStoreProductEvaluationService;
import com.ruoyi.auto.api.RemoteAutoSellerStoreService;
import com.ruoyi.auto.api.domain.ErpAutoSellerStoreConfig;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.order.api.domain.bo.ErpSellerStoreProductEvaluationBo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.erp.order.domain.vo.ErpSellerStoreProductEvaluationVo;
import com.ruoyi.order.api.domain.ErpSellerStoreProductEvaluation;

import javax.servlet.http.HttpServletResponse;
import java.net.MalformedURLException;
import java.util.List;
import java.util.stream.Collectors;
//TODO 挪到order模块中

/**
 * 在线商品测评
 *
 * @author erp
 * @date 2023-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/sellerStoreProductEvaluation")
public class ErpSellerStoreProductEvaluationController extends BaseController {

    private final IErpSellerStoreProductEvaluationService iErpSellerStoreProductEvaluationService;
    private final RemoteAutoSellerStoreService remoteAutoSellerStoreService;

    /**
     * 查看自动测评单列表
     */
    @GetMapping("/selectAutoEvaluationList")
    public TableDataInfo<ErpSellerStoreProductEvaluationVo> selectAutoEvaluationList(ErpSellerStoreProductEvaluationBo evaluation, PageQuery pageQuery) {
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return new TableDataInfo<>();
        }
        if (evaluation.getStoreId() == null) {
            // 获取到用户的店铺和关联的店铺
            R<Object> sellerStoreListByInfo = remoteAutoSellerStoreService.selectAutoStore(userId, null);
            List<ErpAutoSellerStoreConfig> storeConfigList = JSONArray.parseArray(JSONObject.toJSONString(sellerStoreListByInfo.getData()), ErpAutoSellerStoreConfig.class);
            List<Long> list = storeConfigList.stream().map(ErpAutoSellerStoreConfig::getStoreId)
                    .collect(Collectors.toList());
            if (list.isEmpty()) {
                return new TableDataInfo<>();
            }
            evaluation.setStoreList(list);
        }

        return iErpSellerStoreProductEvaluationService.queryPageList(evaluation, pageQuery);
    }
    
    @GetMapping("/judgeSuspected/{aeProductId}/{storeId}")
    public Boolean judgeSuspected(@PathVariable("aeProductId") Long aeProductId, @PathVariable("storeId") Long storeId) {
        return iErpSellerStoreProductEvaluationService.judgeSuspected(aeProductId, storeId);
    }
    @GetMapping("/sevenDaysEvaluation/{aeProductId}")
    Boolean sevenDaysEvaluation(@PathVariable("aeProductId") Long aeProductId) {
        return iErpSellerStoreProductEvaluationService.sevenDaysEvaluation(aeProductId);
    }
     
    @GetMapping("/getEvaluationByOrderId")
    public ErpSellerStoreProductEvaluation getEvaluationByOrderId(@RequestParam("orderId") Long orderId) {
        return iErpSellerStoreProductEvaluationService.getEvaluationByOrderId(orderId);
    }

    /**
     * 新增测评单
     */
    @PostMapping("/addEvaluationOrder")
    public R<Object> addEvaluationOrder(@RequestBody ErpSellerStoreProductEvaluation evaluation) {
        Long storeId = evaluation.getStoreId();
        Long orderId = evaluation.getOrderId();

        return iErpSellerStoreProductEvaluationService.addEvaluationOrder(storeId, orderId);
    }

    /**
     * 作废测评单
     */
    @GetMapping("/InvalidEvaluationOrder")
    public R<Object> invalidEvaluationOrder(String evaluationIds) {
        return iErpSellerStoreProductEvaluationService.invalidEvaluationOrder(evaluationIds);
    }
//
//
//    /**
//     *  标记测评单已线下声明发货
//     */
//    @GetMapping("/evaluationOrderShipped")
//    public R<Object> evaluationOrderShipped(Long evaluationId) {
//        return iErpSellerStoreProductEvaluationService.evaluationOrderShipped(evaluationId);
//    }
//
    /**
     * 店铺选择框列表 测评用
     *
     * @return
     */
    @GetMapping("/selectStoreList")
    public R<Object> selectStoreList() {
        List<String> list = iErpSellerStoreProductEvaluationService.selectStoreList();
        return R.ok(list);
    }

    /**
     * 小红点 获取所有待导出的订单
     */
    @GetMapping("/redReminderForEvaluationOrder")
    public R<Object> redReminderForEvaluationOrder() {
        return iErpSellerStoreProductEvaluationService.redReminderForEvaluationOrder();
    }

    /**
     * 小红点 获取公司测评待导出的订单
     */
    @GetMapping("/redReminderCompany")
    public R<Object> redReminderCompany() {
        return iErpSellerStoreProductEvaluationService.redReminderCompany();
    }

    /**
     * 提交测评
     */
    @PostMapping("/submit")
    public R<Object> submit(@RequestBody ErpSellerStoreProductEvaluationBo bo) {

        return iErpSellerStoreProductEvaluationService.submit(bo);
    }

    /**
     * 填写测评总价
     */
    @GetMapping("/submitTotalAmount")
    public R<Object> submitTotalAmount(String totalAmount, Long evaluationId) {
        return iErpSellerStoreProductEvaluationService.submitTotalAmount(totalAmount,evaluationId);
    }


    /**
     * 填写订单号
     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:submitOrderNumber")
    @GetMapping("/submitOrderNumber")
    public R<Object> submitOrderNumber(
            Long orderNumber,
            Long evaluationId
    ) {
        return iErpSellerStoreProductEvaluationService.submitOrderNumber(orderNumber, evaluationId);
    }

//    /**
//     * 填写国际运单号
//     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:submitIntlTrackingNo")
//    @GetMapping("/submitIntlTrackingNo")
//    public R<Object> submitIntlTrackingNo(
//            String intlTrackingNo,
//            Long evaluationId
//    ) {
//        return iErpSellerStoreProductEvaluationService.submitIntlTrackingNo(intlTrackingNo, evaluationId);
//    }
//
//    /**
//     * 测评单声明发货
//     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:sellerShipmentForEvaluation")
//    @GetMapping("/sellerShipmentForEvaluation")
//    public R<Object> sellerShipmentForEvaluation(
//            Long evaluationId
//    ) {
//        return iErpSellerStoreProductEvaluationService.sellerShipmentForEvaluation(evaluationId);
//    }
//
//
//
    /**
     * 查询在线商品测评列表
     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:list")
    @GetMapping("/list")
    public TableDataInfo<ErpSellerStoreProductEvaluationVo> list(ErpSellerStoreProductEvaluationBo bo, PageQuery pageQuery) {
        return iErpSellerStoreProductEvaluationService.queryPageList(bo, pageQuery);
    }

//    /**
//     * 导出在线商品测评列表
//     */
////    @SaCheckPermission("gather:sellerStoreProductEvaluation:export")
////    @Log(title = "在线商品测评", businessType = BusinessType.EXPORT)
////    @PostMapping("/export")
////    public void export(ErpSellerStoreProductEvaluationBo bo, HttpServletResponse response) {
////        List<ErpSellerStoreProductEvaluationVo> list = iErpSellerStoreProductEvaluationService.queryList(bo);
////        ExcelUtil.exportExcel(list, "在线商品测评", ErpSellerStoreProductEvaluationVo.class, response);
////        for (ErpSellerStoreProductEvaluationVo vo : list) {
////            vo.setStatus(1);
////            iErpSellerStoreProductEvaluationService.updateByBo(BeanUtil.toBean(vo, ErpSellerStoreProductEvaluationBo.class));
////        }
////    }
//
    /**
     * 导出测评信息
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String ids) throws MalformedURLException {
       iErpSellerStoreProductEvaluationService.exportEvaluatio(response,ids);
    }

    @PostMapping("/exportByMobile")
    public void exportByMobile(HttpServletResponse response) throws MalformedURLException {
       iErpSellerStoreProductEvaluationService.exportEvaluatioByMobile(response);
    }

//
//    /**
//     * 获取在线商品测评详细信息
//     *
//     *
//     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:query")
//    @GetMapping("/{id}")
//    public R<ErpSellerStoreProductEvaluationVo> getInfo(@NotNull(message = "主键不能为空")
//                                                        @PathVariable String ids) {
//
//
//        return R.ok();
//    }
//
//    /**
//     * 新增在线商品测评
//     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:add")
//    @Log(title = "在线商品测评", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
//    @PostMapping()
//    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerStoreProductEvaluationBo bo) {
//        return toAjax(iErpSellerStoreProductEvaluationService.insertByBo(bo));
//    }
//
//    /**
//     * 修改在线商品测评
//     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:edit")
//    @Log(title = "在线商品测评", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
//    @PutMapping()
//    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerStoreProductEvaluationBo bo) {
//        return toAjax(iErpSellerStoreProductEvaluationService.updateByBo(bo));
//    }
//
//    /**
//     * 删除在线商品测评
//     *
//     * @param ids 主键串
//     */
//    @SaCheckPermission("gather:sellerStoreProductEvaluation:remove")
//    @Log(title = "在线商品测评", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public R<Void> remove(@NotEmpty(message = "主键不能为空")
//                          @PathVariable Long[] ids) {
//        return toAjax(iErpSellerStoreProductEvaluationService.deleteWithValidByIds(Arrays.asList(ids), true));
//    }

    /**
     * 判断是否是测评单
     */
    @GetMapping("/doubtEvaluation")
    public Integer doubtEvaluation(@RequestParam(value = "country", required = false) String country, @RequestParam(value = "storeId", required = false) Long storeId,
                                   @RequestParam(value = "payTime", required = false) String payTime, @RequestParam(value = "productCount", required = false) Long productCount,
                                   @RequestParam(value = "skuCode", required = false) String skuCode) {
        return iErpSellerStoreProductEvaluationService.doubtEvaluation(country, storeId, payTime, productCount, skuCode);
    }

}
